from django import forms
from web.forms.bootstrap import BootStrap

from web import models
from django.core.exceptions import ValidationError

class GoodModelForm(BootStrap, forms.ModelForm):
    class Meta:
        model = models.Good
        fields = ['name', 'image', 'type', 'desc', 'stock']
        widgets = {
            'desc': forms.Textarea
        }

    def __init__(self, gid=None,  *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.gid = gid

    def clean_image(self):
        image = self.cleaned_data['image']
        if image:
            image.name = self.gid
        return image


class GoodModifyForm(BootStrap, forms.Form):
    type_choices = (
        (1, '生活用品'),
        (2, '食品')
    )
    name = forms.CharField(label='商品名称', max_length=256)
    image = forms.ImageField(label='商品图片')
    type = forms.ChoiceField(label='商品类别', choices=type_choices)
    desc = forms.CharField(label='商品介绍', widget=forms.Textarea)
    stock = forms.IntegerField(label='商品库存', widget=forms.NumberInput(attrs={'min': '0'}))

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)